package com.chdwmp.product.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.chdwmp.product.bean.Product;
import com.chdwmp.product.bean.app.*;
import com.chdwmp.product.bean.backend.AgentVo;
import com.chdwmp.product.constant.Status;
import com.chdwmp.product.exception.CommonException;
import com.chdwmp.product.mapper.AppMapper;
import com.chdwmp.product.service.AppService;
import com.chdwmp.product.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: LLL
 * @Date: 2021/3/13 21:11
 * @Description:
 */
@Service
public class AppServiceImpl implements AppService {


    @Autowired
    AppMapper appMapper;

    @Override
    public AppSystemInfo getAppSystemInfo(String imei, String phone) {
        if(ObjectUtil.isNull(imei))
            throw new CommonException(Status.MISS_REQUIRED_PARAMETER);
        AppSystemInfo appSystemInfo = appMapper.selectAppSystemInfo(imei);
        if(ObjectUtil.isEmpty(appSystemInfo)){
            return new AppSystemInfo();
        }
        return appSystemInfo;
    }

    @Override
    @Transactional
    public boolean getKeepLived(String imei,String serialNumber, String password, String status,String ipAddr) {
        if(ObjectUtil.isNull(imei))
            throw new CommonException(Status.MISS_REQUIRED_PARAMETER);
        boolean result = false;
        if(ObjectUtil.isNull(imei))
            throw new CommonException(Status.MISS_REQUIRED_PARAMETER);
        String current_password = appMapper.selectAppUserPassWord(imei);
        if(false ==  ObjectUtil.equal(password,current_password)){
            //更新用户密码
            appMapper.updateAppUserPassWord(imei,password);
        }
        //查询当前设备状态是否是 黑名单
        String current_status = appMapper.selectAppUserStatus(imei);
        if("1".equals(current_status)){
            result = true;
        }
        //更新设备的状态，以app传过来的为准
        appMapper.updateAppStatus(imei,status,ipAddr);
        //添加到保活表
        int count  = appMapper.queryIsExits(imei,serialNumber);
        if(count == 0){
            appMapper.addAppKeepLived(imei,serialNumber);
        }else{
            appMapper.updateAppKeepLived(imei,serialNumber);
        }
        return result;
    }

    @Override
    public Equipment getEquipment(String imei) {
        if(ObjectUtil.isNull(imei))
            throw new CommonException(Status.MISS_REQUIRED_PARAMETER);
        Equipment equipment = null;
        String user_id  = appMapper.selectAppUserIdByImei(imei);
        List<CameraInfo> cameraInfos = appMapper.selectAppUserCameras(user_id);
        equipment = appMapper.queryAppUserSerialNumber(imei);
        if(ObjectUtil.isNull(equipment)){
            equipment = new Equipment();
        }
        equipment.setCameraInfoList(cameraInfos);
        return equipment;
    }

    @Override
    public void updateEquipment(EquipmentUpdate equipmentUpdate) {
        if(ObjectUtil.isNull(equipmentUpdate.getImei()))
            throw new CommonException(Status.MISS_REQUIRED_PARAMETER);
        String user_id  = appMapper.selectAppUserIdByImei(equipmentUpdate.getImei());
        equipmentUpdate.setUserId(user_id);
        appMapper.updateAppUserCameraInfo(equipmentUpdate);
    }

    @Override
    public String getUserAppCurrentStatus(String imei) {
        if(ObjectUtil.isNull(imei))
            throw new CommonException(Status.MISS_REQUIRED_PARAMETER);
        return appMapper.selectAppUserCurrentStatus(imei);
    }

    @Override
    public List<Products> getProductList() {
        return appMapper.selectProductList();
    }

    @Override
    public List<Products> getUserProducts(String imei) {
        if(ObjectUtil.isNull(imei))
            throw new CommonException(Status.MISS_REQUIRED_PARAMETER);
        Products product = null;
        List<Products> list = new ArrayList<>();
        String user_id  = appMapper.selectAppUserIdByImei(imei);
        String product_id = appMapper.selectAppUserByProducts(imei);
        if(ObjectUtil.isNotNull(product_id) && product_id.contains(",")){
            //说明购买多个产品
            List<String> stringList = StringUtil.stringConveList(product_id);
            if(ObjectUtil.isNotNull(stringList)){
                for (String s : stringList) {
                    product = appMapper.selectProductById(s);
                    list.add(product);
                }
            }
        } else{
            list = appMapper.selectAppUserByProductList(user_id);
        }
        if(ObjectUtil.isNull(list)){
            list = new ArrayList<>();
        }
        return list;
    }

    @Override
    public boolean isActivation(Activation activation) {
        if(ObjectUtil.isNull(activation.getImei()) || ObjectUtil.isNull(activation.getSerial_number()))
            throw new CommonException(Status.MISS_REQUIRED_PARAMETER);
        String id  = appMapper.selectSerialNumberIdByCode(activation.getSerial_number());
        if(ObjectUtil.isNull(id)){
            return false;
        }else{
            AppSystemInfoAdd appSystemInfoAdd = new AppSystemInfoAdd();
            appSystemInfoAdd.setImei(activation.getImei());
            appSystemInfoAdd.setSerialNumber(id);
            appSystemInfoAdd.setPhone(activation.getPhone());
            appSystemInfoAdd.setPrefixCode(activation.getPrefix_code());
            appSystemInfoAdd.setFirstClassCustomerName(activation.getFirst_class_customer_name());
            appSystemInfoAdd.setSecondClassCustomerName(activation.getSecond_class_customer_name());
            appSystemInfoAdd.setPassword(activation.getPassword());
            appSystemInfoAdd.setIsActivationCode("1");
            appSystemInfoAdd.setProductId(activation.getProduct_id());
            appSystemInfoAdd.setActivationaryTime(LocalDateTime.now().toString());
            int count  = appMapper.addAppSystemInfo(appSystemInfoAdd);
            if(count > 0){
                return true;
            }
        }
        return false;
    }

    @Override
    public List<Product> getALlProductList() {
        List<Product> list = appMapper.queryAllProductList();
        return list;
    }

    @Override
    public List<AgentProductVo> getAgentProductList(String name) {
        List<AgentProductVo> result = new ArrayList<>();
        List<Integer> id = appMapper.queryAgentIdByName(name);
        if(null != id && id.size() > 0){
            for (Integer i : id) {
                List<AgentProductVo> result1 = new ArrayList<>();
                result1 = appMapper.getAgentProductList(i);
                if(null != result1){
                    for (AgentProductVo vo : result1) {
                        int count = appMapper.queryBuyCount(vo.getProductId(),i);
                        vo.setCount(count);
                    }
                }
                result.addAll(result1);
            }
        }
        return result;
    }

    @Override
    public List<AgentVo> getAllAgentList() {
        return appMapper.getAllAgentList();
    }

}
